﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using B10.Models;
using Microsoft.Security.Application;
using B10.Helpers;
using Telerik.Web.Mvc;

namespace B10.Controllers
{
    [HandleError]
    public class ForumController : Controller
    {
        IDatabaseRepository _db;

        public ForumController()
            : this(new DatabaseRepository())
        { }

        public ForumController(IDatabaseRepository db)
        {
            _db = db;
        }


        //
        // GET: /Forum/
        public ActionResult Index()
        {
            return View();
        }


        [GridAction]
        public ActionResult _PostList()
        {
            var posts = _db.GetAll<ForumPost>().Where(p => !p.ParentId.HasValue);
            //return View(posts);
            return View(new GridModel<ForumPost>()
            {
                Data = posts
            });
        }

        public ActionResult Post(int id)
        {
            ForumPost post = _db.GetPost(id);
            if (post == null)
                return new FileNotFoundResult();
            return View(post);
        }

        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection collection)
        {
            UserProfile profile = _db.GetProfile(UserHelper.UserId);
            if (profile == null)
                return new FileNotFoundResult();
            ForumPost post = new ForumPost();
            post.Date = DateTime.Now;
            post.UserProfile = profile;
            if (collection.AllKeys.Contains("Title"))
            {
                post.Title = Server.HtmlEncode(collection["Title"]);
            }
            if (collection.AllKeys.Contains("Body"))
            {
                post.Body = AntiXssHtmlHelper.GetSafeHtml(collection["Body"]);
            }
            if (collection.AllKeys.Contains("replyId"))
            {
                string replyId = collection["replyId"];
                int repId;
                if (!String.IsNullOrEmpty(replyId) && Int32.TryParse(replyId, out repId))
                {
                    ForumPost parent = _db.GetPost(repId);
                    if (parent != null && parent.ForumPost1 == null)
                    {
                        post.Title = String.Format("Re: {0}", parent.Title);
                        post.ForumPost1 = parent;
                    }
                }
            }
            if (ModelState.IsValid)
            {
                _db.Save();
                if (post.ForumPost1 == null)
                    return RedirectToAction("Post", new { id = post.Id });
                else
                    return RedirectToAction("Post", new { id = post.ParentId });
            }
            return View();
        }

    }
}
